//
//  DHQAuthorizeStatusMgr.m
//  ZounianMerchantPods
//
//  Created by 杜宏曲 on 2017/12/4.
//  Copyright © 2017年 king. All rights reserved.
//

#import "DHQAuthorizeStatusMgr.h"
#import "DHQDeviceInfoMgr.h"
#import<Photos/Photos.h>
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVCaptureDevice.h>
//#import <AVFoundation/AVFoundation.h>

@implementation DHQAuthorizeStatusMgr

DHQSingleTokenMacroM(StatusMgr);

#pragma mark - 麦克风
- (BOOL)canRecord {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
        return NO;
    } else {
        return YES;
    }
}

- (void)canMicrophoneRecordAuthorityWithHandleBlock:(void(^)())handle {
    NSString *authorityName = @"麦克风";
    NSString *authorityPath = @"Privacy&path=MICROPHONE";
    if ([self canRecord]) {
        if (handle) handle();
    } else {
        [self showAlertViewWithauthorityName:authorityName authorityPath:authorityPath];
    }
}

#pragma mark - 相机
- (BOOL)canRecordViedo {
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
        return NO;
    } else {
        return YES;
    }
}

- (void)canRecordViedoAuthorityWithHandleBlock:(void(^)())handle {
    NSString *authorityName = @"相机";
    NSString *authorityPath = @"Privacy&path=CAMERA";
    if ([self canRecordViedo]) {
        if (handle) handle();
    } else {
        [self showAlertViewWithauthorityName:authorityName authorityPath:authorityPath];
    }
}

#pragma mark - 相册
- (BOOL)canAlbumAuthority {

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status ==PHAuthorizationStatusDenied){
        return NO;
    } else {
        return YES;
    }
}

- (void)canAlbumAuthorityWithHandleBlock:(void(^)())handle {
    NSString *authorityName = @"相册";
    NSString *authorityPath = @"Privacy&path=PHOTOS";
    if ([self canAlbumAuthority]) {
        if (handle) handle();
    } else {
        [self showAlertViewWithauthorityName:authorityName authorityPath:authorityPath];
    }
}

#pragma mark - 定位
- (BOOL)canLocationAuthority {
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (![CLLocationManager locationServicesEnabled]
        || status == kCLAuthorizationStatusDenied
        || status == kCLAuthorizationStatusRestricted) {
        return NO;
    } else {
        return YES;
    }
}

- (void)canLocationAuthorityWithHandleBlock:(void(^)())handle {
    NSString *authorityName = @"定位服务";
    NSString *authorityPath = @"Privacy&path=LOCATION";
    
    if ([self canLocationAuthority]) {
        if (handle) handle();
    } else {
        [self showAlertViewWithauthorityName:authorityName authorityPath:authorityPath];
    }
}

#pragma mark - 拼接提示语
- (NSString *)fullTipsString:(NSString *)authorityName {
    return [NSString stringWithFormat:@"请您设置允许APP访问您的%@\n设置->隐私->%@",authorityName, authorityName];
}

#pragma mark - 弹系统提示框
- (void)showAlertViewWithauthorityName:(NSString *)authorityName authorityPath:(NSString *)authorityPath {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                     message:[self fullTipsString:authorityName]
                                                              preferredStyle:UIAlertControllerStyleAlert];
    WEAKSELF;
    UIAlertAction *config = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        STRONGSELF;
        [self handelAppSettingMatchPhoneVersion:authorityPath];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
    
    [alertVC addAction:config];
    [alertVC addAction:cancel];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - 设置跳转
// 进权限列表页
- (void)handelAuthorityListMatchPhoneVersion:(NSString *)authorityPath {
    NSString *handleString;
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    if (phoneVersion.floatValue < 10.0) { //iOS10 以前使用 prefs:root=
        handleString = [NSString stringWithFormat:@"prefs:root=%@", authorityPath];
    } else { //iOS10 以后使用 App-Prefs:root=
        handleString = [NSString stringWithFormat:@"App-Prefs:root=%@", authorityPath];
    }
    
    NSURL *url = [NSURL URLWithString:handleString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url
                                           options:@{}
                                 completionHandler:nil];
    }
}

// 进APP设置页
- (void)handelAppSettingMatchPhoneVersion:(NSString *)authorityPath {
    
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url
                                           options:@{}
                                 completionHandler:nil];
    }
}

@end
